package cool.qingqing.exception;

import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import cool.qingqing.result.Result;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 高山青
 * @Date 2021/12/26  -   0:10
 */
@Slf4j
@RestControllerAdvice
public class ExceptionsHandler {
	/**
	 * 业务异常
	 *
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(RuntimeException.class)
	public Result bizExceptionHandler(RuntimeException ex) {
		log.debug("ExceptionsHandler class  "+ex.toString());
		return new Result(ex.getMessage());
	}



	/**
	 * 参数转换异常拦截
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(HttpMessageNotReadableException.class)
	public Result exceptionHandler(HttpMessageNotReadableException ex) {
		log.debug("ExceptionsHandler class  "+ex.toString());
		return new Result("参数格式异常！");
	}

	/**
	 * 参数转换异常拦截
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(MissingServletRequestParameterException.class)
	public Result exceptionHandler(MissingServletRequestParameterException ex) {
		log.debug("ExceptionsHandler class  "+ex.toString());
		return new Result("请求参数错误，请联系管理员！");
	}
}
